package com.example.demo.bean;

import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ControllerAdviceTest {

//    private static final Logger LOGGER = LoggerFactory.getLogger(ControllerAssist.class);

    @ExceptionHandler({ThisSystemException.class})
    public @ResponseBody
    JsonData handleBusinessException(ThisSystemException exception, HttpServletRequest request) {
//        LOGGER.info("业务异常 : {}", exception.getMessage(), exception);
//        Logger logger = new Logger("handleBusinessException: " + request.getSession().getId());
//        System.out.println("request" + request.getRequestURI());
        return JsonData.fail(exception.getCode(), exception.getMessage());
    }

    /**
     * 处理请求参数验证异常
     *
     * @param exception
     * @param request
     * @return
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public  @ResponseBody JsonData handleMethodArgumentNotValidException(MethodArgumentNotValidException exception, HttpServletRequest request) {
        return JsonData.fail(ErrorCode.VALUE_NULL.getStatus(), exception.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }


}
